package com.xszx.dao.impl;

import com.xszx.bean.Goods;
import com.xszx.dao.GoodsDao;
import com.xszx.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public void addGoods(Goods goods) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "insert into t_goods values(null, ?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, goods.getGoodsName());
            ps.setDouble(2, goods.getGoodsPrice());
            ps.setString(3, goods.getGoodsImageUrl());
            ps.setTimestamp(4, new Timestamp(new java.util.Date().getTime()));
            ps.setInt(5, goods.getGoodsState());
            ps.setString(6, goods.getGoodsInfo());
            ps.setInt(7, goods.getGoodsType());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<Goods> getAllGoods(boolean state) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_goods where goodsstate = 1";
            if (state) {
                sql += " limit 0, 8";
            }
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet resultSet = ps.executeQuery();

            List<Goods> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String goodsName = resultSet.getString("goodsname");
                Double goodsPrice = resultSet.getDouble("goodsprice");
                String goodsImageUrl = resultSet.getString("goods_imageurl");
                Date goodsTime = resultSet.getDate("goodstime");
                Integer goodsState = resultSet.getInt("goodsstate");
                String goodsInfo = resultSet.getString("goodsInfo");
                Integer goodsType = resultSet.getInt("goodstype");
                list.add(new Goods(id, goodsName, goodsPrice, goodsImageUrl, goodsTime, goodsState, goodsInfo, goodsType));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Goods> getAllGoodsAdmin(boolean state) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_goods";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet resultSet = ps.executeQuery();

            List<Goods> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String goodsName = resultSet.getString("goodsname");
                Double goodsPrice = resultSet.getDouble("goodsprice");
                String goodsImageUrl = resultSet.getString("goods_imageurl");
                Date goodsTime = resultSet.getDate("goodstime");
                Integer goodsState = resultSet.getInt("goodsstate");
                String goodsInfo = resultSet.getString("goodsInfo");
                Integer goodsType = resultSet.getInt("goodstype");
                list.add(new Goods(id, goodsName, goodsPrice, goodsImageUrl, goodsTime, goodsState, goodsInfo, goodsType));
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Goods getGoodsById(Integer id) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_goods where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                String goodsName = resultSet.getString("goodsname");
                Double goodsPrice = resultSet.getDouble("goodsprice");
                String goodsImageUrl = resultSet.getString("goods_imageurl");
                Date goodsTime = resultSet.getDate("goodstime");
                Integer goodsState = resultSet.getInt("goodsstate");
                String goodsInfo = resultSet.getString("goodsInfo");
                Integer goodsType = resultSet.getInt("goodstype");
                return new Goods(id, goodsName, goodsPrice, goodsImageUrl, goodsTime, goodsState, goodsInfo, goodsType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void updateGoods(Goods goods) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_goods set goodsname = ?, goodsprice = ?, goods_imageurl = ?, goodstime = ?, goodsInfo = ?, goodstype = ? where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, goods.getGoodsName());
            ps.setDouble(2, goods.getGoodsPrice());
            ps.setString(3, goods.getGoodsImageUrl());
            ps.setDate(4, goods.getGoodsTime());
            ps.setString(5, goods.getGoodsInfo());
            ps.setInt(6, goods.getGoodsType());
            ps.setInt(7, goods.getId());
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateGoodsState(Integer id, Integer state) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_goods set goodsstate = ?, goodstime = ?  where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, state);
            ps.setTimestamp(2, new Timestamp(new java.util.Date().getTime()));
            ps.setInt(3, id);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
